﻿#include "GLLInc.h"
#include "GLLResMgr.h"
#include "GLLLevelManager.h"

#ifdef GLL_OGRE
#ifdef GLL_WIN
namespace GLL
{
	void ResMgr::UnloadLevelRes_spec(Level& lvl)
	{
		Ogre::ResourceGroupManager& rgm = Ogre::ResourceGroupManager::getSingleton();
		rgm.unloadResourceGroup(lvl._strID);
	}

	void ResMgr::LoadLevelRes_spec(Level& lvl)
	{
		Ogre::ResourceGroupManager& rgm = Ogre::ResourceGroupManager::getSingleton();
		StringStream lvlPath;
		lvlPath << _lvlsPath << lvl._id << "\\";
		String strLvlPath(lvlPath.str());

		lvlPath << "*";
		String findPath(lvlPath.str());
		WIN32_FIND_DATA findData;
		HANDLE hndl = FindFirstFileA(findPath.c_str(), &findData);
		BOOL found = ( hndl != INVALID_HANDLE_VALUE);

		//遍历所有目录
		while(found)
		{
			StringStream folder;
			folder << strLvlPath << findData.cFileName;
			String strFolder(folder.str());
			rgm.addResourceLocation(strFolder, "FileSystem", lvl._strID);
			found = FindNextFileA(hndl, &findData);
		}

		rgm.initialiseResourceGroup(lvl._strID);
	}

	void ResMgr::Create()
	{
		char dirPath[MAX_PATH]; 
		GetCurrentDirectory(MAX_PATH, dirPath);
		{
			StringStream strm;
			strm << dirPath << "\\";
			_appPath = strm.str();
		}
		{
			StringStream strm;
			strm << _appPath << "resource\\";
			 _resPath = strm.str();
		}
		{
			StringStream strm;
			strm << _resPath << "Levels\\";
			 _lvlsPath= strm.str();
		}
	}

	void ResMgr::Destory()
	{
	}
	//-----------------------------------------------------------------------------
	void _initAllResource()
	{
		// Load resource paths from config file
		Ogre::ConfigFile cf;
		cf.load( "resources.cfg" );

		// Go through all settings in the file
		Ogre::ConfigFile::SectionIterator itSection = cf.getSectionIterator();

		Ogre::String sSection, sType, sArch;
		while( itSection.hasMoreElements() )
		{
			sSection = itSection.peekNextKey();

			Ogre::ConfigFile::SettingsMultiMap *mapSettings = itSection.getNext();
			Ogre::ConfigFile::SettingsMultiMap::iterator itSetting = mapSettings->begin();
			while( itSetting != mapSettings->end() ) 
			{
				sType = itSetting->first;
				sArch = itSetting->second;
				Ogre::ResourceGroupManager::getSingleton().addResourceLocation(sArch, sType, sSection );
				++itSetting;
			}
		}
		Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
	}


};//GLL

#endif GLL_WIN
#endif GLL_OGRE